package serie04;

public class StdContact implements Contact {
	
	// ATTRIBUTS
	
	private String lastName;
	private String firstName;
	private Civ civility;
	
	// CONSTRUCTEURS
	
	public StdContact(String n, String p) {
		this(Civ.UKN, n, p);
	}
	
	public StdContact(Civ c, String n, String p) {
		if (c == null) {
			throw new IllegalArgumentException("Mauvaise civilite");
		}
		if (n == null) {
			throw new IllegalArgumentException("Mauvais nom");
		}
		if (p == null) {
			throw new IllegalArgumentException("Mauvais prenom");
		}
		if (n.equals("") && p.equals("")) {
			throw new IllegalArgumentException("Les 2 chaines sont vides");
		}
		civility = c;
		lastName = n;
		firstName = p;
	}
	
    // REQUETES
	
    public int compareTo(Contact c) {
    	if (c == null) {
    		throw new NullPointerException("Le contact comparé mauvais");
    	}
    	int compLastName = this.getLastName().compareTo(c.getLastName());
    	int compFirstName = this.getFirstName().compareTo(c.getFirstName());
    	int compCivility = this.getCivility().compareTo(c.getCivility());
    	if (compLastName < 0 || compLastName == 0 && compFirstName < 0
    			|| compLastName == 0 && compFirstName == 0 
    			    && compCivility < 0) {
    		return -1;
    	} else if (compLastName == 0 && compFirstName == 0
    			    && compCivility == 0) {
    		return 0;
    	} else {
    		return 1;
    	}
    }
    
    public boolean equals(Object obj) {
    	if (obj == null) {
    		return false;
    	}
    	if (this.getClass() != obj.getClass()) {
    		return false;
    	}
    	Contact c = (Contact) obj;
    	return this.getLastName().equals(c.getLastName()) 
    			&& this.getFirstName().equals(c.getFirstName())
    			&& this.getCivility().equals(c.getCivility());
    }
    
    public Civ getCivility() {
    	return civility;
    }
    
    public String getFirstName() {
    	return firstName;
    }
    
    public String getLastName() {
    	return lastName;
    }
    
    public int hashCode() {
    	return toString().hashCode();
    }
    
    public String toString() {
    	return getCivility() + " " + getFirstName() + " " + getLastName();
    }
    
    // COMMANDES
    
    public void setCivility(Civ civility) {
    	if (!getCivility().canEvolveTo(civility)) {
    		throw new IllegalArgumentException(
    				"Impossible de modifier la civilité"
    		);
    	}
    	this.civility = civility;
    }
}
